package com.xzw.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xzw.mapper.ClazzMapper;
import com.xzw.pojo.*;
import com.xzw.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 条件分页查询班级
     */
    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        // 1.设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());
        // 2.执行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);
        // 3.解析查询结果，并封装
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(p.getTotal(), p.getResult());
    }
    /**
     * 删除班级
     */
    @Override
    public void delete(Integer id) {
        clazzMapper.deleteById(id);
    }
    /**
     * 新增班级
     */
    @Override
    public void save(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.insert(clazz);
    }
    /**
     * 根据ID查询班级信息 -- 查询回显功能
     */
    @Override
    public Clazz getInfo(Integer id) {
        return clazzMapper.getById(id);
    }
    /**
     * 修改班级信息
     */
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.updateById(clazz);
    }
    /**
     * 查询所有班级
     */
    @Override
    public List<Clazz> list() {
        List<Clazz> clazzList = clazzMapper.list(null);
        if (clazzList != null) {
            return clazzList;
        }
        throw new RuntimeException("查询班级列表失败");
    }
}

